<?php
//ini_set('display_errors', 1); // set to 0 for production version 
//error_reporting(E_ALL);
chong_pha_hoai();
class class__kiem_tra_form_de_them_du_lieu
{

	function chuoi_nhieu_hinh_anh()
	{
		$mang=getDirectoryList("../hinhanh_flash/upload_tam/");
		// ham getDirectoryList :lay danh sach file trong thu muc , tra ve mang
		$chuoi="";
		for($i=0;$i<count($mang);$i++)
		{
			$chuoi=$chuoi.$mang[$i]."____";
		}
		if($chuoi=="____")
		{
			$chuoi="";
		}
		return $chuoi;
	}
	function them_vao_co_so_du_lieu_mot_dong($id){
		chong_90($ten_file_anh);
		$c3="";
		if($_FILES['up_hinh_1']['name']!=""){chong_90($_FILES['up_hinh_1']['name']);$c3=$c3."____".$_FILES['up_hinh_1']['name'];}
		if($_FILES['up_hinh_2']['name']!=""){chong_90($_FILES['up_hinh_2']['name']);$c3=$c3."____".$_FILES['up_hinh_2']['name'];}
		if($_FILES['up_hinh_3']['name']!=""){chong_90($_FILES['up_hinh_3']['name']);$c3=$c3."____".$_FILES['up_hinh_3']['name'];}
		if($_FILES['up_hinh_4']['name']!=""){chong_90($_FILES['up_hinh_4']['name']);$c3=$c3."____".$_FILES['up_hinh_4']['name'];}
		if($_FILES['up_hinh_5']['name']!=""){chong_90($_FILES['up_hinh_5']['name']);$c3=$c3."____".$_FILES['up_hinh_5']['name'];}
		if($_FILES['up_hinh_6']['name']!=""){chong_90($_FILES['up_hinh_6']['name']);$c3=$c3."____".$_FILES['up_hinh_6']['name'];}
		if($_FILES['up_hinh_7']['name']!=""){chong_90($_FILES['up_hinh_7']['name']);$c3=$c3."____".$_FILES['up_hinh_7']['name'];}
		if($_FILES['up_hinh_8']['name']!=""){chong_90($_FILES['up_hinh_8']['name']);$c3=$c3."____".$_FILES['up_hinh_8']['name'];}
		if($_FILES['up_hinh_9']['name']!=""){chong_90($_FILES['up_hinh_9']['name']);$c3=$c3."____".$_FILES['up_hinh_9']['name'];}
		if($_FILES['up_hinh_10']['name']!=""){chong_90($_FILES['up_hinh_10']['name']);$c3=$c3."____".$_FILES['up_hinh_10']['name'];}
		if($_FILES['up_hinh_11']['name']!=""){chong_90($_FILES['up_hinh_11']['name']);$c3=$c3."____".$_FILES['up_hinh_11']['name'];}
		if($_FILES['up_hinh_12']['name']!=""){chong_90($_FILES['up_hinh_12']['name']);$c3=$c3."____".$_FILES['up_hinh_12']['name'];}
		//if($_FILES['up_hinh_1']['name']!=""){if(ereg(".php"))}
		// ham empty_forder : lam rong thu muc
		//$chuoi_nhieu_hinh_anh=$this->chuoi_nhieu_hinh_anh();
		$_SESSION['size']=trim($_POST['size']);
		$_SESSION['capdo']=trim($_POST['capdo']);
		/*
		$tv="select max(id) from dulieu";
		$tv_1=mysql_query($tv);
		$tv_2=mysql_fetch_row($tv_1);
		$id=$tv_2[0]+1;*/
		$thu_muc="../hinhanh_flash/sanpham/$id/";
		//mkdir($thu_muc);
		//chmod($thu_muc, 0777);
		
		$ten_file_anh=$id."/".$_FILES['uploadedfile']['name'];
		$_POST['size']=trim($_POST['size']);
		$_POST['mo_ta_ngan']=nl2br($_POST['mo_ta_ngan']);
		if(trim($_POST['keywords'])==""){$_POST['keywords']=trim($_POST['tieude']);}
		if(isset($_POST['khuyenmai'])) $is_promotion=1;
		if(isset($_POST['thanhly'])) $is_liquidation=1;
		if(isset($_POST['banchay'])) $is_sohot=1;
		if(isset($_POST['trangchu'])) $is_home=1;
		$chuoi_dulieu_themvao="
		INSERT INTO `dulieu`
		(
			 `id` , `ma_sp`,`tieu_de` , `gia` ,`giadeal`,`is_promotion`,`is_liquidation`,`is_sohot`,`is_home`,
			 `loai_gia`, `hinh_anh` ,`noi_dung` , `thuoc_menu`,`trang_thai`,`khuyen_mai`,`nhieu_hinh_anh`,
			 `tieu_de__en`,`noi_dung__en` ,`trang_thai__en`,`khuyen_mai__en`,`size`,`keywords`,`description`,`khoi_luong`,`mo_ta_ngan`
		 )
		VALUES
		(
			NULL ,'$_POST[ma_sp]','$_POST[tieude]', '$_POST[giaban]','$_POST[giadeal]','$is_promotion',
			 '$is_liquidation','$is_sohot','$is_home','$_POST[loai_gia]', '$ten_file_anh',
			 '$_POST[noidung]', '$_POST[capdo]','$_POST[trang_thai]','$_POST[khuyen_mai]','$c3',
			 '$_POST[tieude_en]','$_POST[noidung_en]','$_POST[trang_thai__en]','$_POST[khuyen_mai__en]','$_POST[size]',
			 '$_POST[keywords]','$_POST[description]','$_POST[khoi_luong]','$_POST[mo_ta_ngan]'
		);";
		//die($chuoi_dulieu_themvao);
		
			mysql_query($chuoi_dulieu_themvao);
			
			//create_file($thu_muc."index.html");
			if($_FILES['up_hinh_1']['name']!=""){move_uploaded_file($_FILES['up_hinh_1']['tmp_name'],$thu_muc.$_FILES['up_hinh_1']['name']);}
			if($_FILES['up_hinh_2']['name']!=""){move_uploaded_file($_FILES['up_hinh_2']['tmp_name'],$thu_muc.$_FILES['up_hinh_2']['name']);}
			if($_FILES['up_hinh_3']['name']!=""){move_uploaded_file($_FILES['up_hinh_3']['tmp_name'],$thu_muc.$_FILES['up_hinh_3']['name']);}
			if($_FILES['up_hinh_4']['name']!=""){move_uploaded_file($_FILES['up_hinh_4']['tmp_name'],$thu_muc.$_FILES['up_hinh_4']['name']);}
			if($_FILES['up_hinh_5']['name']!=""){move_uploaded_file($_FILES['up_hinh_5']['tmp_name'],$thu_muc.$_FILES['up_hinh_5']['name']);}
			if($_FILES['up_hinh_6']['name']!=""){move_uploaded_file($_FILES['up_hinh_6']['tmp_name'],$thu_muc.$_FILES['up_hinh_6']['name']);}
			if($_FILES['up_hinh_7']['name']!=""){move_uploaded_file($_FILES['up_hinh_7']['tmp_name'],$thu_muc.$_FILES['up_hinh_7']['name']);}
			if($_FILES['up_hinh_8']['name']!=""){move_uploaded_file($_FILES['up_hinh_8']['tmp_name'],$thu_muc.$_FILES['up_hinh_8']['name']);}
			if($_FILES['up_hinh_9']['name']!=""){move_uploaded_file($_FILES['up_hinh_9']['tmp_name'],$thu_muc.$_FILES['up_hinh_9']['name']);}
			if($_FILES['up_hinh_10']['name']!=""){move_uploaded_file($_FILES['up_hinh_10']['tmp_name'],$thu_muc.$_FILES['up_hinh_10']['name']);}
			if($_FILES['up_hinh_11']['name']!=""){move_uploaded_file($_FILES['up_hinh_11']['tmp_name'],$thu_muc.$_FILES['up_hinh_11']['name']);}
			if($_FILES['up_hinh_12']['name']!=""){move_uploaded_file($_FILES['up_hinh_12']['tmp_name'],$thu_muc.$_FILES['up_hinh_12']['name']);}
			//mkdir("$tentaikhoan");
			//chmod("$tentaikhoan", 0777);

	}
	function upload_anh($id){
		//mysql_query($chuoi_dulieu_themvao);
		/*
		$tv="select max(id) from dulieu";
		$tv_1=mysql_query($tv);
		$tv_2=mysql_fetch_row($tv_1);
		$id=$tv_2[0]+1;*/

		$thu_muc="../hinhanh_flash/sanpham/$id/";
		//mkdir($thu_muc);
		//chmod($thu_muc, 0777);
		
		$ten_file_anh=$id."/".$_FILES['uploadedfile']['name'];
		//$ten_file_anh=$_FILES['uploadedfile']['name'];
		$duongdan_upload_anh="../hinhanh_flash/sanpham/".$ten_file_anh;
		chong_90($ten_file_anh);
		move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$duongdan_upload_anh);
		
	}
	function maxid() {
		$ftpacc="shop20k";
		$ftppwd="123456789";
		
		$tv="select max(id) from dulieu";
		$tv_1=mysql_query($tv);
		$tv_2=mysql_fetch_row($tv_1);
		$id=$tv_2[0]+1;
		
		
		$dir = "../hinhanh_flash/sanpham/$id";
		$check = is_dir($dir);
		
		###Begin for hosting ##
		if($check == FALSE)
		{
			if(ini_get('safe_mode') == 'Off')
			{
				$old_umask = umask(0);
				mkdir("$dir", 0777) or die('could not create directory ->off');
				umask($old_umask);
				//mkdir($dir);
				//chmod($dir, 0777);
			}
			else
			{
				$connect = ftp_connect('103.3.244.78') or die('<p>Connection to FTP failed.</p>');
				$login = ftp_login($connect, $ftpacc, $ftppwd) or die('<p>Could not login to FTP.</p>');
				$changeDir = ftp_chdir($connect, '/domains/20k.com.vn/public_html/hinhanh_flash/sanpham/') or die('<p>Could not change directory.</p>');
				$makeDir = ftp_mkdir($connect, "$id") or die('<p>Could not created directory.</p>');
				$old_umask = umask(0);
				$setPerm = ftp_site($connect, "CHMOD 0777 /domains/20k.com.vn/public_html/hinhanh_flash/sanpham/$id/") or die('<p>Could not set permissions.</p>');
				umask($old_umask);
				ftp_close($connect);
			}
		}
		##End for hosting
		
		##Begin for local
		//mkdir($dir);
		//chmod($dir, 0777);
		##End for local
		return $id;
	}
	function them_du_lieu(){
		$id=$this->maxid();
		$this->upload_anh($id);
		$this->them_vao_co_so_du_lieu_mot_dong($id);
		//die();
	}
	function kiem_tra_tieu_de_form(){
			if($_POST['tieude']!="" && $_FILES['uploadedfile']['name']!="")
			{
				$this->them_du_lieu();
			}
			else
			{
				thongbao("Không được bỏ trống tiêu đề hoặc hình ảnh");
			}
	}
	function xacdinh_upload_khong_trung_ten_anh(){
		$ten_file_anh=$_FILES['uploadedfile']['name'];
		$chuoi_tv="select count(*) from dulieu where hinh_anh in ('$ten_file_anh')";
		//thongbao($chuoi_tv);
		$mang_so_dem_so_anh=mysql_fetch_row(mysql_query($chuoi_tv));
		$soanh_demduoc=$mang_so_dem_so_anh[0];
		$soanh_demduoc=0;
		if($soanh_demduoc==0)
		{
			return "co";
		}
		else
		{
			if($ten_file_anh=="")
			{
				return "co";
			}
			else
			{
				return "khong";
			}
		}
	}
	function kiem_tra_hinh_anh_form(){
		$xacdinh_upload_khong_trung_ten_anh=$this->xacdinh_upload_khong_trung_ten_anh();
		if($xacdinh_upload_khong_trung_ten_anh=="co")
		{
			$this->kiem_tra_tieu_de_form();
		}
		else
		{
			thongbao("File ảnh này trùng tên với tên file ảnh đã up len , xin bạn hãy đổi tên file ảnh !");
		}
	}
	function kiem_tra_form_de_them_du_lieu()
	{
		if(isset($_POST['them_dulieu_menu_doc']))
		//neu co ton tai bien $_POST['them_dulieu_menu_doc'] thi moi duoc kiem tra hinh anh form gui di co hop le hay khong
		{
			$this->kiem_tra_hinh_anh_form();
			//neu co ton tai bien $_POST['them_dulieu_menu_doc'] thi moi duoc kiem tra hinh anh form gui di co hop le hay khong
		}
	}
}
$class__kiem_tra_form_de_them_du_lieu=new class__kiem_tra_form_de_them_du_lieu;
	$class__kiem_tra_form_de_them_du_lieu->kiem_tra_form_de_them_du_lieu();
	//echo ereg("a","abc");
?>
